//
//  CubeViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/11.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CubeViewController.h"
#import <GLKit/GLKit.h>

#define LIGHT_DIRECTION 0, 1, -0.5
#define AMBIENT_LIGHT 0.5

@interface CubeViewController ()

@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UILabel *numberOneLabel;
@property (nonatomic, strong) UILabel *numberTwoLabel;
@property (nonatomic, strong) UILabel *numberThreeLabel;
@property (nonatomic, strong) UILabel *numberFourLabel;
@property (nonatomic, strong) UILabel *numberFiveLabel;
@property (nonatomic, strong) UILabel *numberSixLabel;
@property (nonatomic, strong) UIButton *clickButtonOnNumberThree; // 测试点击事件

@property (nonatomic, assign) CATransform3D myTransfrom3D;

@end

@implementation CubeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 布局
    [self addMySubview];
}

// 布局
- (void)addMySubview
{
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backgroundView];
    
    self.numberOneLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.numberOneLabel.text = @"1";
    self.numberOneLabel.userInteractionEnabled = YES;
    self.numberOneLabel.textAlignment = NSTextAlignmentCenter;
    self.numberOneLabel.backgroundColor = [UIColor whiteColor];
    self.numberOneLabel.textColor = [UIColor redColor];
    self.numberOneLabel.font = [UIFont systemFontOfSize:30];
    [self.backgroundView addSubview:_numberOneLabel];
    
    self.numberTwoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.numberTwoLabel.text = @"2";
    self.numberTwoLabel.userInteractionEnabled = YES;
    self.numberTwoLabel.textAlignment = NSTextAlignmentCenter;
    self.numberTwoLabel.backgroundColor = [UIColor whiteColor];
    self.numberTwoLabel.textColor = [UIColor redColor];
    self.numberTwoLabel.font = [UIFont systemFontOfSize:30];
    [self.backgroundView addSubview:_numberTwoLabel];
    
    self.numberThreeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.numberThreeLabel.text = @"3";
    self.numberThreeLabel.userInteractionEnabled = YES;
    self.numberThreeLabel.textAlignment = NSTextAlignmentCenter;
    self.numberThreeLabel.backgroundColor = [UIColor whiteColor];
    self.numberThreeLabel.textColor = [UIColor redColor];
    self.numberThreeLabel.font = [UIFont systemFontOfSize:30];
    [self.backgroundView addSubview:_numberThreeLabel];
    
    self.clickButtonOnNumberThree = [UIButton buttonWithType:UIButtonTypeCustom];
    self.clickButtonOnNumberThree.frame = CGRectMake(50, 50, 100, 100);
    self.clickButtonOnNumberThree.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.1];
//    self.clickButtonOnNumberThree.layer.shadowColor = [UIColor greenColor].CGColor;
//    self.clickButtonOnNumberThree.layer.shadowOpacity = 1;
//    self.clickButtonOnNumberThree.layer.shadowOffset = CGSizeMake(0, 0);
    [self.clickButtonOnNumberThree addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.numberThreeLabel addSubview:_clickButtonOnNumberThree];
    
    self.numberFourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.numberFourLabel.text = @"4";
    self.numberFourLabel.userInteractionEnabled = YES;
    self.numberFourLabel.textAlignment = NSTextAlignmentCenter;
    self.numberFourLabel.backgroundColor = [UIColor whiteColor];
    self.numberFourLabel.textColor = [UIColor redColor];
    self.numberFourLabel.font = [UIFont systemFontOfSize:30];
    [self.backgroundView addSubview:_numberFourLabel];
    
    self.numberFiveLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.numberFiveLabel.text = @"5";
    self.numberFiveLabel.userInteractionEnabled = YES;
    self.numberFiveLabel.textAlignment = NSTextAlignmentCenter;
    self.numberFiveLabel.backgroundColor = [UIColor whiteColor];
    self.numberFiveLabel.textColor = [UIColor redColor];
    self.numberFiveLabel.font = [UIFont systemFontOfSize:30];
    [self.backgroundView addSubview:_numberFiveLabel];
    
    self.numberSixLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.numberSixLabel.text = @"6";
    self.numberSixLabel.userInteractionEnabled = YES;
    self.numberSixLabel.textAlignment = NSTextAlignmentCenter;
    self.numberSixLabel.backgroundColor = [UIColor whiteColor];
    self.numberSixLabel.textColor = [UIColor redColor];
    self.numberSixLabel.font = [UIFont systemFontOfSize:30];
    [self.backgroundView addSubview:_numberSixLabel];
    
    // 为每一个数字面添加3D仿射
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D.m34 = - 1.0 / 500.0;
    self.backgroundView.layer.sublayerTransform = transform3D;
    
    CATransform3D transform3DForNumberOne = CATransform3DMakeTranslation(0, 0, 100);
    [self addTransform:transform3DForNumberOne toView:self.numberOneLabel];
    
    CATransform3D transform3DForNumberTwo = CATransform3DMakeTranslation(100, 0, 0);
    transform3DForNumberTwo = CATransform3DRotate(transform3DForNumberTwo, M_PI_2, 0, 1, 0);
    [self addTransform:transform3DForNumberTwo toView:self.numberTwoLabel];
    
    CATransform3D transform3DForNumberThree = CATransform3DMakeTranslation(0, -100, 0);
    transform3DForNumberThree = CATransform3DRotate(transform3DForNumberThree, M_PI_2, 1, 0, 0);
    [self addTransform:transform3DForNumberThree toView:self.numberThreeLabel];
    
    CATransform3D transform3DForNumberFour = CATransform3DMakeTranslation(0, 100, 0);
    transform3DForNumberFour = CATransform3DRotate(transform3DForNumberFour, -M_PI_2, 1, 0, 0);
    [self addTransform:transform3DForNumberFour toView:self.numberFourLabel];
    
    CATransform3D transform3DForNumberFive = CATransform3DMakeTranslation(-100, 0, 0);
    transform3DForNumberFive = CATransform3DRotate(transform3DForNumberFive, -M_PI_2, 0, 1, 0);
    [self addTransform:transform3DForNumberFive toView:self.numberFiveLabel];
    
    CATransform3D transform3DForNumberSix = CATransform3DMakeTranslation(0, 0, -100);
    transform3DForNumberSix = CATransform3DRotate(transform3DForNumberSix, M_PI, 0, 1, 0);
    [self addTransform:transform3DForNumberSix toView:self.numberSixLabel];
    
    
    // 对父视图的subLayerTransform进行3D旋转
    transform3D = CATransform3DRotate(transform3D, -M_PI_4, 1, 0, 0);
    transform3D = CATransform3DRotate(transform3D, -M_PI_4, 0, 1, 0);
    self.backgroundView.layer.sublayerTransform = transform3D;
    self.myTransfrom3D = transform3D;
    
    // 当我们点击数字3面上的button发现不响应，原因是因为4、5、6层的响应事件的顺序较高，即把hit test with event事件阻截掉了，解决的办法是将4、5、6的userInteractionEnabled设置为NO
    self.numberFourLabel.userInteractionEnabled = NO;
    self.numberFiveLabel.userInteractionEnabled = NO;
    self.numberSixLabel.userInteractionEnabled = NO;
    
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint currentPoint = [[touches anyObject] locationInView:self.view];
    CGPoint previousPoint = [[touches anyObject] previousLocationInView:self.view];
    CGFloat gapX = currentPoint.x - previousPoint.x;
    CGFloat gapY = currentPoint.y - previousPoint.y;
    if (gapX != 0) {
        CATransform3D transform3D = CATransform3DIdentity;
                transform3D = CATransform3DRotate(self.myTransfrom3D, gapX / 100, 0, 1, 0);
                self.backgroundView.layer.sublayerTransform = transform3D;
           self.myTransfrom3D = transform3D;
    }
    if (gapY != 0) {
        CATransform3D transform3D = CATransform3DIdentity;
                transform3D = CATransform3DRotate(self.myTransfrom3D, -gapY / 100, 1, 0, 0);
                self.backgroundView.layer.sublayerTransform = transform3D;
            self.myTransfrom3D = transform3D;
    }
}



- (void)addTransform:(CATransform3D)transform3D toView:(UILabel *)label
{
    CGSize containerSize = self.backgroundView.frame.size;
    label.center = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
//    NSLog(@"容器的大小：%@", [NSValue valueWithCGSize:containerSize]);
//    NSLog(@"中心点：%@", [NSValue valueWithCGPoint:label.center]);
    label.layer.transform = transform3D;
    [self addShadowToLayer:label.layer];
}

- (void)addShadowToLayer:(CALayer *)face
{
    CALayer *layer = [CALayer layer];
    layer.frame = face.bounds;
    [face addSublayer:layer];
    //convert the face transform to matrix
    //(GLKMatrix4 has the same structure as CATransform3D)
    //译者注：GLKMatrix4和CATransform3D内存结构一致，但坐标类型有长度区别，所以理论上应该做一次float到CGFloat的转换，感谢[@zihuyishi](https://github.com/zihuyishi)同学~
    CATransform3D transform = face.transform;
    GLKMatrix4 matrix4 = *(GLKMatrix4 *)&transform;
    GLKMatrix3 matrix3 = GLKMatrix4GetMatrix3(matrix4);
    //get face normal
    GLKVector3 normal = GLKVector3Make(0, 0, 1);
    normal = GLKMatrix3MultiplyVector3(matrix3, normal);
    normal = GLKVector3Normalize(normal);
    //get dot product with light direction
    GLKVector3 light = GLKVector3Normalize(GLKVector3Make(LIGHT_DIRECTION));
    float dotProduct = GLKVector3DotProduct(light, normal);
    NSLog(@"dotProduct = %f",dotProduct);
    //set lighting layer opacity
    CGFloat shadow = 1 + dotProduct - AMBIENT_LIGHT;
    NSLog(@"shadow = %f", shadow);
    UIColor *color = [UIColor colorWithWhite:0 alpha:shadow];
    layer.backgroundColor = color.CGColor;
}

// 点击数字3界面上的按钮，点击以后变成蓝色
- (void)clickButton:(UIButton *)button
{
    NSLog(@"点击了");
    [[[UIAlertView alloc] initWithTitle:@"提示" message:@"点击3的button了" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show ];
    self.clickButtonOnNumberThree.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.1];
}




@end
